package org.flexlancer.databases
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class Products extends EventDispatcher
	{
		private static const TABLE:String = "products";
		public function Products(target:IEventDispatcher=null)
		{
			super(target);
		}
		public static function List(condition:String = ""):Array{
			try{
				var productList:Array = DatabaseManager.Manager.Select(TABLE,null,condition);
				
				for each ( var singleProduct:Object in productList ){
					var suppid:String = singleProduct.refsupplier;
					var suppArray:Array = Suppliers.List("supplierid = "+suppid);
					if ( suppArray.length > 0 ){
						singleProduct.suppliername = suppArray[0].suppliername;
					}
					
					var productid:String = singleProduct.productid;
					var ppArray:Array = ProductPrice.List("refid LIKE 'PROD-"+productid+"'");
					var completeprice:String = "";
					for each ( var singlePrice:Object in ppArray ){
						if ( completeprice != "" ) completeprice += ", ";
						completeprice += singlePrice.price;
					}
					singleProduct.completeprice = completeprice;
					singleProduct.prices = ppArray;
				}
				
				return productList;
			}catch(err:Error){
				throw err;
			}
			return null;
		}
		public static function Add(
				productTitle:String, 
				barcode:String, 
				description:String, 
				instock:Number, 
				orderlevel:Number, 
				cost:Number, 
				producttypeid:String,
				supplierid:String,
				unitid:String,
				price:Array):void{

			try{//customerid INTEGER PRIMARY KEY AUTOINCREMENT","customername TEXT", "customertype TEXT", "credit REAL"]);
				DatabaseManager.Manager.Begin();
				var custObj:Object = new Object();
				custObj.title = " '" + productTitle + "' ";
				custObj.barcode =  " '" + barcode + "' ";
				custObj.desc = " '" + description + "' ";
				custObj.instock = instock;
				custObj.orderlevel = orderlevel;
				custObj.cost = cost;
				
				custObj.refsupplier = supplierid;
				custObj.refproducttype = producttypeid;
				custObj.refunit = unitid;
				
				DatabaseManager.Manager.Insert(TABLE, custObj);
				var insertID:Number = DatabaseManager.Manager.LastInsertRowID;
				//var recs:Array = DatabaseManager.Manager.Select("products");
				//insertID = recs[insertID-1].productid;
				
				if (price){
					var arrPrices:Array = new Array();
					for each ( var singlePrice:Object in price ){
						//["refid TEXT","contacttype TEXT", "contactdetail TEXT"]
						var contObj:Object = new Object();
						contObj.refid = " 'PROD-"+insertID+"' ";
						contObj.title = " '"+singlePrice.title+"' ";
						contObj.price = singlePrice.price;
						contObj.markup = singlePrice.markup;
						arrPrices.push(contObj);
					}
					ProductPrice.Add(arrPrices);
				}

				DatabaseManager.Manager.Commit();
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}
		public static function Delete(condition:String = ""):void{
			try{
				DatabaseManager.Manager.Delete(TABLE,condition);
			}catch(err:Error){
				throw err;
			}
		}
		public static function Update(
				productID:String,
				productTitle:String, 
				barcode:String, 
				description:String, 
				instock:Number, 
				orderlevel:Number, 
				cost:Number, 
				producttypeid:String,
				supplierid:String,
				unitid:String,
				price:Array):void{

			try{//customerid INTEGER PRIMARY KEY AUTOINCREMENT","customername TEXT", "customertype TEXT", "credit REAL"]);
				DatabaseManager.Manager.Begin();
				var custObj:Object = new Object();
				custObj.title = " '" + productTitle + "' ";
				custObj.barcode =  " '" + barcode + "' ";
				custObj.desc = " '" + description + "' ";
				custObj.instock = instock;
				custObj.orderlevel = orderlevel;
				custObj.cost = cost;
				
				custObj.refsupplier = supplierid;
				custObj.refproducttype = producttypeid;
				custObj.refunit = unitid;
				
				DatabaseManager.Manager.Update(TABLE, custObj , "productid = " + productID);
				
				if (price){
					var arrPrices:Array = new Array();
					for each ( var singlePrice:Object in price ){
						//["refid TEXT","contacttype TEXT", "contactdetail TEXT"]
						var contObj:Object = new Object();
						contObj.refid = " 'PROD-"+productID+"' ";
						contObj.title = " '"+singlePrice.title+"' ";
						contObj.price = singlePrice.price;
						contObj.markup = singlePrice.markup;
						arrPrices.push(contObj);
					}
					ProductPrice.Delete("refid LIKE 'PROD-"+productID+"'");
					ProductPrice.Add(arrPrices);
				}

				DatabaseManager.Manager.Commit();
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}
	}
}